package Dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import pojo.SysUser;

import java.io.InputStream;
import java.util.List;


public class SysUserMapperTest {
private Logger logger = Logger.getLogger(SysUserMapperTest.class);
public void  count() throws Exception{
SqlSession sqlSession = null;
try{
    //1读取MyBatis框架的核心配置文件
    String resource = "mybatis-config.xml";
    //获取对mybatis-config.xml的输入流
    InputStream is = Resources.getResourceAsStream(resource);
    //使用SQLserssionFactoryBuilder读取配置文件
    //SqlsessionFactoreBuiLder实例
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
    //创建sqlSession实例
    sqlSession = factory.openSession();
    //创建SysUserMapper接口实例，调用其方法执行sql语句
    int count = sqlSession.getMapper(SysUserMapper.class).count();

}finally {
    //关闭SqlSession
    sqlSession.close();
}
}
public  void testUserListQuery() throws Exception{
    SqlSession sqlSession = null;
    List<SysUser> userList =null;
    try{
        //1.读取MyBatis框架的核心配置文件
        String resource = "mybatis-config.xml";
        //获取对MyBatis-config.xml的输入流
        InputStream is = Resources.getResourceAsStream(resource);
        //2.使用SqlSessionFactory读取配置文件并构建
        //SqlSessionFactory实例
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3.创建SqlSession
        sqlSession = factory.openSession();
        //4.创建SysUserMapper接口实例,调用其他方法执行相关的SQL语句
        userList = sqlSession.getMapper(SysUserMapper.class).getUserList();
    } finally {
        //5.关闭SqlSession
        sqlSession.close();
    }
    if (userList != null) {
        for (SysUser user : userList) {
            System.out.println("testUserListQuery account:" + user.getAccount() + "and realName:"
                    + user.getRealName());
        }
    }
}
}

